package Model.QBE;

import Model.SQL.*;
import Model.Beans.*;
import java.util.*;
import java.sql.*;
import Model.Beans.Results.*;

public class ClassroomModel extends DataAccessHelperBase{
    
    public ClassroomModel() 
    {
        String tableName = "classroom";        
        super.customize(tableName);
    }
    
    public int insert(ClassRoom class_room)
    {
        String[] values = new String[3];
        values[0] = "null";
        values[1] = "'"+ class_room.getBuilding() + "'";
        values[2] = "'" + Integer.toString(class_room.getNumber()) + "'";
        
        return this.insertRecord(values);
    }
    
    public int delete(String id)
    {
        return this.deleteRecord("classroom_id =" + id); 
    }
    
    public int delete(ClassRoom class_room)
    {
        return this.delete(Integer.toString(class_room.getId()));
    }
    
    public int modify(String id, String building, String number)
    {
        String id_str= "classroom_id = " + id;
        String[] values = new String[2];
        int i = 0;
        if(building.length() > 0)
            values[i++] = "building = '" + building +"'";
        if(number.length() > 0)
            values[i++] =  "number = '" + number + "'";
       return this.updateRecord(id_str, values);
    }
    
    public int modify(ClassRoom class_room)
    {
        return this.modify(Integer.toString(class_room.getId()), class_room.getBuilding(), Integer.toString(class_room.getNumber()));
    }
    
    public ClassRoomList search(String id, String building, String number)
    {
        String where = "";
        ResultSet rs = null;
        ClassRoomList list = new ClassRoomList();
        
        if(id.length() > 0 && !id.equals("0"))
            where += "classroom_id = " + id + " AND ";
        if(building.length() > 0)
            where += "building = " + "'" + building + "' AND ";
        if(number.length() > 0 && !number.equals("0"))
            where += "number = " + number;
        else if(where.length() > 0)
            where = where.substring(0, where.length()-4);
        rs = this.getData(where);
        
        if(rs == null)
            return new ClassRoomList();
        try 
        {
            while(rs.next()) 
            {
                int _id = rs.getInt("classroom_id");
                String _building = rs.getString("building");
                int _number = rs.getInt("number");
                
                ClassRoom tmp = new ClassRoom();
                tmp.setId(_id);
                tmp.setBuilding(_building);
                tmp.setNumber(_number);
                
                list.addClass(tmp);
                //Debugging purpose
                System.out.println(_id + "   " + _building +  "   " + _number);
            }
            return list;
        }
        catch(SQLException e)
        {
            System.out.println("SQLException: " + e.getMessage());
        }   
        finally
        {
            if (rs != null) 
            {
                try 
                {
                    rs.close();
                } catch (SQLException sqlEx) { }
                rs = null;
            }
        }
        return new ClassRoomList();
    }
    
    public ClassRoomList search(ClassRoom class_room)
    {
        return this.search(Integer.toString(class_room.getId()), class_room.getBuilding(), Integer.toString(class_room.getNumber()));
    }
    
    public ClassRoom search_by_id(String id)
    {
        ClassRoom classroom = new ClassRoom();
        classroom.setId(Integer.parseInt(id));
        
        ClassRoomList list = new ClassRoomList();
        list = this.search(classroom);
        if(list.getClassrooms().size() > 0)
            return (ClassRoom)(list.getClassrooms().get(0));
        else
            return null;
    }
        
    /*
    public static void main(String[] args)
    {
        ClassroomModel m = new ClassroomModel();
        ClassRoom classs = new ClassRoom();
        classs.setBuilding("NE");
        classs.setNumber(10);
        
        m.search("","03","15");
        ClassRoomList list = m.search(new ClassRoom());
        m.search(classs);
        
        classs = m.search_by_id("5");
        
        System.out.println(classs.toString());
        
        
        for(int i=0; i< list.getClassrooms().size(); i++)
        {
            System.out.println((ClassRoom)(list.getClassrooms().get(i)));
        }
        
        System.out.println(list.toXML());
    }
    */
}
